package com.springBoot.springBootSysCore.modules.controller.web.system;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.springBoot.springBootSysCore.common.persistence.DataTables;
import com.springBoot.springBootSysCore.common.utils.FileUtil;
import com.springBoot.springBootSysCore.common.utils.returnUtil.AjaxJson;
import com.springBoot.springBootSysCore.common.utils.returnUtil.PageUtils;
import com.springBoot.springBootSysCore.modules.controller.web.base.BaseController;
import com.springBoot.springBootSysCore.modules.entity.system.SysIntro;
import com.springBoot.springBootSysCore.modules.enums.base.DeleteStateEnum;
import com.springBoot.springBootSysCore.modules.services.system.SysIntroService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 *app引导图
 */
@Controller
@RequestMapping("${adminPath}/sysIntro")
public class SysIntroController extends BaseController<SysIntro,String> {
    @Autowired
    private SysIntroService sysIntroService;

    @ModelAttribute("entity")
    public SysIntro get(@RequestParam(required = false) String id) {
        if (StringUtils.isNotBlank(id)) {
            return sysIntroService.findById(id);
        } else {
            SysIntro in = new SysIntro();
            return in;
        }
    }
    /**
     * 分页查询
     * @param request
     * @param model
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping("queryByPage")
    @ResponseBody
    public Object queryByPage(HttpServletRequest request,
                              Model model) throws JsonProcessingException{
        DataTables dt =  DataTables.getInstance(request, null);
        return sysIntroService.queryByPageForDataTable(request,dt,PageUtils.buildPageRequest(dt.getStart()/dt.getLength() + 1,dt.getLength(),
                "sort", "desc"));
    }

    @RequestMapping(value="findOne",method= RequestMethod.GET)
    public String findOne(String id,Model model){
        return "system/intro/introChange";
    }
    /**
     * 保存
     * @param entity
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value="saveEntity",method=RequestMethod.POST)
    @ResponseBody
    public Object saveEntity(SysIntro entity, Model model, HttpServletResponse response,HttpServletRequest request) throws JsonProcessingException{
        if (!beanValidator(model, entity)){
            return renderString(response, new AjaxJson().setMsg(model.asMap().get("message").toString()).setSuccess(false));
        }
        Map<String, String> m = FileUtil.upLoadFile("userfiles/sysIntro", request);
        if(StringUtils.isNotBlank(m.get("file"))){
            entity.setImg(m.get("file"));
        }
        return sysIntroService.save(entity);
    }

    @RequestMapping(value="deleteEntity",method=RequestMethod.POST)
    @ResponseBody
    public Object deleteEntity(SysIntro entity) throws JsonProcessingException{
        return sysIntroService.deleteEntity(entity);
    }

}
